package com.baymax.test.scene.demo

import android.graphics.Color
import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.children
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.drawable.ColorUtils
import com.baymax.test.scene.newSceneFlexContainer
import kotlin.math.max
import kotlin.math.min

class HSViewScene : BaseScene() {
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newSceneFlexContainer {
            val hs = object :HorizontalScrollView(requireSceneContext()) {

                override fun requestChildFocus(child: View?, focused: View?) {
                    super.requestChildFocus(child, focused)
                    println("requestChildFocus  ${child}")
                }

                override fun requestRectangleOnScreen(rectangle: Rect?): Boolean {
                    println("requestRectangleOnScreen  ${findFocus()}")
                    return super.requestRectangleOnScreen(rectangle)
                }

                override fun computeScrollDeltaToGetChildRectOnScreen(rect: Rect): Int {

                    val width = width
                    val screenLeft = scrollX
                    val screenRight = screenLeft + width
                    val startX = screenLeft + 100
                    var scrollXDelta = 0
                    if (rect.right > startX && rect.left > startX) {
                        // need to move right to get it in view: move right just enough so
                        // that the entire rectangle is in view (or at least the first
                        // screen size chunk).
                        //move right

                        if (rect.width() > width) {
                            // just enough to get screen size chunk on
                            scrollXDelta += (rect.left - startX)
                        } else {
                            // get entire rect at right of screen
                            scrollXDelta += (rect.right - startX - rect.width() )
                        }

                        // make sure we aren't scrolling beyond the end of our content
                        val right = getChildAt(0).right
                        val distanceToRight = right - screenRight
                        scrollXDelta = min(scrollXDelta, distanceToRight)
                    } else if (rect.left < startX && rect.right < screenRight) {
                        // need to move right to get it in view: move right just enough so that
                        // entire rectangle is in view (or at least the first screen
                        // size chunk of it).
                        //move left

                        if (rect.width() > width) {
                            // screen size chunk
                            scrollXDelta -= (startX - rect.right)
                        } else {
                            // entire rect at left
                            scrollXDelta -= (startX - rect.left)
                        }

                        // make sure we aren't scrolling any further than the left our content
                        scrollXDelta = max(scrollXDelta, -scrollX)
                    }

                    println("r $rect ,x $scrollXDelta")

                    return  scrollXDelta
                }


            }
            val content = LinearLayout(requireSceneContext())
            hs.addView(content)
            addView(hs, -1, 100)

            (0..30).forEach {
                content.addView(createTxt(it), LinearLayout.LayoutParams(100 ,100).apply {
                    marginStart = 10
                    marginEnd = 10
                })
            }

            addButton("first"){
                content.children.first().requestFocus()
            }

            addButton("last"){
                content.children.last().requestFocus()
            }
        }
    }

    private fun createTxt(i: Int): TextView {
        return TextView(requireSceneContext()).apply {
            minWidth = 100
            text = "I($i)"
            isFocusable = true
            setTextColor(ColorUtils.createColorStateList(Color.BLUE, Color.BLACK))
        }
    }
}